/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelComprarFornecedor.java
 *
 * Created on 21/11/2011, 18:25:07
 */
package all_pet_iv.gui;

import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author bruna
 */
import all_pet_iv.webservice.ConexaoForce;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Item__c;
public class PanelComprarFornecedor extends javax.swing.JPanel {

    /** Creates new form PanelComprarFornecedor */
    public PanelComprarFornecedor() {
        initComponents();
        carregarTabela();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane = new javax.swing.JScrollPane();
        tabelaCompras = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButtonSelecionar = new javax.swing.JButton();

        tabelaCompras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "Tipo", "Quantidade", "Métrica", "Preço(R$)"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane.setViewportView(tabelaCompras);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("COMPRAS");

        jButtonSelecionar.setText("Selecionar");
        jButtonSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSelecionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addComponent(jButtonSelecionar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonSelecionar)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void carregarTabela(){
        try {
            EnterpriseConnection con = ConexaoForce.getConexao();
            QueryResult qr = con.query("SELECT codigoProduto__c,Name,tipo__c,Quantidade__c,metrica__c,precoUnitario__c FROM Item__c ORDER BY codigoProduto__c");
            SObject[] records = qr.getRecords();
            DefaultTableModel modelo = (DefaultTableModel) this.tabelaCompras.getModel();
            for(int i=0;i<records.length;i++){
                modelo.addRow(new Object[]{null, null, null, null, null, null});
                Item__c item = (Item__c) qr.getRecords()[i];
                String codigoProduto = item.getCodigoProduto__c();
                String name = item.getName();
                String tipo = item.getTipo__c();
                Double qtd = item.getQuantidade__c();
                String metrica = item.getMetrica__c();
                Double preco = item.getPrecoUnitario__c();
                
                modelo.setValueAt(codigoProduto, i, 0);
                modelo.setValueAt(name, i, 1);
                modelo.setValueAt(tipo, i, 2);
                modelo.setValueAt(qtd, i, 3);
                modelo.setValueAt(metrica, i, 4);
                modelo.setValueAt(preco, i, 5);
                
            }
        } catch (ConnectionException ex) {
            JOptionPane.showMessageDialog(null, "Não foi possível se conectar com o fornecedor. Tente novamente mais tarde!", "ERRO", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private void jButtonSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelecionarActionPerformed
        int x = tabelaCompras.getSelectedRow();
        if (x == -1 || tabelaCompras.getModel().getValueAt(x, 0) == null) {
            JOptionPane.showMessageDialog(null, "Escolha um produto!", "Aviso", JOptionPane.WARNING_MESSAGE);
        } else {
            new FrameFinalizarCompras().setVisible(true);
        }
            
    }//GEN-LAST:event_jButtonSelecionarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonSelecionar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTable tabelaCompras;
    // End of variables declaration//GEN-END:variables

    public JTable getTable() {
        return this.tabelaCompras;
    }
}
